	package com.tpu.daoclasses;
	import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
	/**
	* A layer supertype that handles the common operations for all
	* Data Access Objects.
	*/
public class BdDao {

	private Session session;
	private Transaction tx;
	public BdDao() {
	HibernateFactory.buildSessionFactory();
	}
	public void saveOrUpdate(Object obj) {
	try {
	startOperation();
	session.saveOrUpdate(obj);
	tx.commit();
	} catch (HibernateException e) {
	handleException(e);
	} finally {
	HibernateFactory.close(session);
	}
	}
	public void Save(Object obj) {
		try {
		startOperation();
		session.save(obj);
		tx.commit();
		} catch (HibernateException e) {
		handleException(e);
		} finally {
		}
		}
	public Session getSession() {
		session = HibernateFactory.openSession();
		return session;
		}
	public List MySqlQuery(String txt) {
		List txtout=new ArrayList();
		try {
		startOperation();
		txtout=session.createSQLQuery(txt).list();
		tx.commit();
		} catch (HibernateException e) {
		handleException(e);
		} finally {
		HibernateFactory.close(session);
		}
		return txtout;
		}
	public void Delete(Object obj) {
	try {
	startOperation();
	session.delete(obj);
	tx.commit();
	} catch (HibernateException e) {
	handleException(e);
	} finally {
	HibernateFactory.close(session);
	}
	}
	public Object Find(Class clazz,int id) {
	Object obj = null;
	try {
	startOperation();
	obj = session.load(clazz, id);
	tx.commit();
	} catch (HibernateException e) {
	handleException(e);
	} finally {
	HibernateFactory.close(session);
	}
	return obj;
	}
	public Object Getobject(Class clazz,int id) {
		Object obj = null;
		try {
		startOperation();
		obj = session.get(clazz, id);
		tx.commit();
		} catch (HibernateException e) {
		handleException(e);
		} finally {
		HibernateFactory.close(session);
		}
		return obj;
		}
	public List FindAll(Class clazz) {
	List objects = null;
	try {
	startOperation();
	Query query = session.createQuery(
	"from " + clazz.getName());
	objects = query.list();
	tx.commit();
	} catch (HibernateException e) {
	handleException(e);
	} finally {
	HibernateFactory.close(session);
	}
	return objects;
	}
	public void handleException(HibernateException e)
	throws HibernateException {
	HibernateFactory.rollback(tx);
	throw new HibernateException(e);
	}
	public void startOperation()
	throws HibernateException {
	session = HibernateFactory.openSession();
	tx = session.beginTransaction();
	}
	public void endOperation()
	throws HibernateException {
		HibernateFactory.close(session);
	}
	
}
